// 
// CustomDrawnPanel.cs
//  
// Author:
//       Tomas Vondracek <tomas.vondracek@gmail.com>
// 
// Copyright (c) 2010 Tomas Vondracek
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Common.Gui.GuiWinFormsLib.Controls
{
	/// <summary>
	/// color changed handler for controls 
	/// </summary>
	internal delegate void ColorChangedHandler(object sender, EventArgs e);

	public class CustomDrawnPanel : UserControl
	{
		private Color highlightColor;
		private Color tempColor;

		private Color color1;

		/// <summary>
		/// gradient's color 1
		/// </summary>
		public Color Color1
		{
			get { return color1; }
			set
			{
				color1 = value;
				ColorChanged();
			}
		}
		private Color color2;

		/// <summary>
		/// gradient's color 2
		/// </summary>
		public Color Color2
		{
			get { return color2; }
			set
			{
				color2 = value;
				ColorChanged();
			}
		}

		public Color HighlightColor
		{
			get
			{
				return this.highlightColor;
			}
			set
			{
				this.highlightColor = value;
				ColorChanged();
			}
		}

		private Brush gradBrush;

		internal event ColorChangedHandler OnColorChanged;

		public CustomDrawnPanel()
		{
			this.color1 = SystemColors.ControlDark;
			this.color2 = SystemColors.ControlLight;
			this.highlightColor = Color.GreenYellow;

			this.Paint += new PaintEventHandler(CustomDrawnPanel_Paint);
			this.OnColorChanged += new ColorChangedHandler(CustomDrawnPanel_OnColorChanged);
			this.Load += new EventHandler(CustomDrawnPanel_Load);

			this.MouseEnter += delegate
			{
				this.tempColor = this.color1;
				this.color1 = this.highlightColor;
				ColorChanged();
			};

			this.MouseLeave += delegate
			{
				this.color1 = this.tempColor;
				ColorChanged();
			};
		}

		void CustomDrawnPanel_Load(object sender, EventArgs e)
		{
			// init colors
			ColorChanged();
		}

		private void CustomDrawnPanel_OnColorChanged(object sender, EventArgs e)
		{
			// check for invalid rectangle
			if (this.Width > 0 && this.Height > 0)
			{
				this.gradBrush = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), this.color2, this.color1, LinearGradientMode.Vertical);
				Invalidate();
			}
		}

		/// <summary>
		/// call OnColorChanged event if possible 
		/// </summary>
		private void ColorChanged()
		{
			if (this.OnColorChanged != null)
				OnColorChanged(this, EventArgs.Empty);
		}

		private void CustomDrawnPanel_Paint(object sender, PaintEventArgs e)
		{
			Graphics g = e.Graphics;
			if (this.gradBrush != null && this.Width > 0 && this.Height > 0)
			{
				g.FillRectangle(this.gradBrush, 0, 0, this.Width, this.Height);
			}
		}

		private void InitializeComponent()
		{
			this.SuspendLayout();
			// 
			// CustomDrawnPanel
			// 
			this.DoubleBuffered = true;
			this.Name = "CustomDrawnPanel";
			this.ResumeLayout(false);

		}
	}
}